const api = require('../../config/supplierDetail.js');
const apiFee = require('../../config/home.js');
const apiPay = require('../../config/api.js');
const apiCollect = require('../../config/api.js');
const {
  phoneNumber
} = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tag: false,
    showFFXX: false,
    currentType: '',
    collectValue: 0,
    ffxxData: {},
    // htmlSnip:'',
    id: '',
    list: '',
    achievement: ''
  },
  onClick(e) { },
  onClose() {
    this.setData({})
  },
  getDictDataList() {
    wx.request({
      url: apiFee.dictDataListUrl,
      data: {
        type: 'miniprogram_fee'
      },
      method: 'GET',
      success: (res) => {
        console.log(res)
        if (res.data.code === 200) {
          let fee = {};
          let data = res.data.data
          data.forEach(element => {
            let key = element.dictLabel
            let value = element.dictValue
            if (key == "telephone") {
              fee.telephone = value
            } else if (key == "adminLicenseCheck") {
              fee.adminLicenseCheck = value
            } else if (key == "basicDetails") {
              fee.basicDetails = value
            } else if (key == "exceptionCheck") {
              fee.exceptionCheck = value
            } else if (key == "seniorPerson") {
              fee.seniorPerson = value
            }
          });
          this.setData({
            fee
          })
        }
      }
    });
  },
  getPhoneNumber() {
    if (this.data.phoneNumber == this.data.list.phoneName) {
      return;
    }
    console.log(this.data.fee)
    this.toPhoneNumberPay("enterprise pay", parseFloat(this.data.fee.telephone))

  },
  initDetail() {
    const userid = wx.getStorageSync('wxUser') ? wx.getStorageSync('wxUser').id : ''
    if (!userid) {
      wx.switchTab({
        url: '/pages/my/index'
      })
    }
    wx.request({
      url: api.detail,
      data: {
        id: this.data.id,
        userId: userid
      },
      method: 'GET',
      success: (res) => {
        if (res.data.code === 100) {
          let obj = res.data.object.enterpriseEntity
          let collectEntity = res.data.object.collectEntity
          let achievement = obj.achievement ? obj.achievement.replace(/<img/g, '<img class="richImg"') : ''
          let phoneNumber = obj.phone
          if (obj && obj.phone && obj.phone.length > 3) {
            obj.phoneName = obj.phone.substring(0, 3) + '********'
          } else {
            obj.phoneName = ''
          }
          this.setData({
            achievement: achievement,
            list: obj,
            phoneNumber: phoneNumber,
            collectValue: collectEntity
          })
        } else if (res.data.code === 200) {
          wx.showToast({
            title: res.data.msg
          });
        }

      }
    });
  },
  toPhoneNumberPay(description, fee) {
    let this_ = this;
    let openid = wx.getStorageSync('openid')
    wx.request({
      url: apiPay.wxPay,
      method: 'GET',
      data: {
        openid: openid,
        amount: fee,
        description: description,
        vipGradeId: -1
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 200) {
          this_.phoneNumberPay(JSON.parse(res.data.data))
        } else {
          wx.showToast({
            title: "网络异常！"
          })
        }

      },
      fail: (res) => {

        wx.hideLoading();
      }
    })
  },
  phoneNumberPay(param) {
    var that = this;
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {
        that.setData({
          "list.phoneName": that.data.phoneNumber
        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  toGradePay(description, fee) {
    let this_ = this;
    let openid = wx.getStorageSync('openid')
    wx.request({
      url: apiPay.wxPay,
      method: 'GET',
      data: {
        openid: openid,
        amount: fee,
        description: description,
        vipGradeId: -1
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 200) {
          this_.setData({
            param: res.data.data
          })
          this_.pay(JSON.parse(res.data.data))
        } else {
          wx.showToast({
            title: "网络异常！"
          })
        }

      },
      fail: (res) => {

        wx.hideLoading();
      }
    })
  },
  //支付
  pay: function (param) {
    var that = this;
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {

        that.getSFData()
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  getSFData() {
    let type = this.data.currentType
    wx.showLoading({
      title: '加载中…'
    })
    switch (type) {
      case '1':
        this.requestDJG()
        break;
      case '2':
        this.requestQYGSXQ()
        break;
      case '3':
        this.requestJYYC()
        break;
      case '4':
        this.requestXZXK()
        break;
    }
  },
  requestQYGSXQ() {
    wx.request({
      url: apiPay.getBasicDetailsByName,
      method: 'GET',
      data: {
        keyword: this.data.list.fullName
      },
      success: (res) => {
        wx.hideLoading();
        let data = res.data || {}
        if (data && data.code == '200' && data.data) {
          this.setData({
            showFFXX: true,
            ffxxData: data.data
          })
        } else {
          wx.showToast({
            title: data.msg || "网络异常！"
          })
        }

      },
      fail: (res) => {
        wx.hideLoading();
      }
    })
  },
  requestDJG() {
    wx.request({
      url: apiPay.getDJGUrl,
      method: 'GET',
      data: {
        searchKey: this.data.list.fullName,
        personName: this.data.list.legalPerson,
        pageIndex: 0,
        pageSize: 100
      },
      success: (res) => {
        wx.hideLoading();
        let data = res.data || {}
        if (data && data.code == '200' && data.data && data.data.length) {
          this.setData({
            showFFXX: true,
            ffxxData: data.data
          })
        } else {
          wx.showToast({
            title: data.msg || "网络异常！"
          })
        }

      },
      fail: (res) => {

        wx.hideLoading();
      }
    })
  },
  requestJYYC() {
    wx.request({
      url: apiPay.getExceptionCheck,
      method: 'GET',
      data: {
        searchKey: this.data.list.fullName
      },
      success: (res) => {
        wx.hideLoading();
        let data = res.data || {}
        if (data && data.code == '200') {
          let list = data.data || []
          if (list && list.length) {
            list.forEach(x => {
              x.AddDate = x.AddDate.substring(0, 10)
            })
          }
          this.setData({
            showFFXX: true,
            ffxxData: list
          })
        } else {
          wx.showToast({
            title: data.msg || "网络异常！"
          })
        }

      },
      fail: (res) => {

        wx.hideLoading();
      }
    })
  },
  requestXZXK() {
    
    wx.request({
      url: apiPay.getAdminLicenseCheck,
      method: 'GET',
      data: {
        searchKey: this.data.list.fullName
      },
      success: (res) => {
        console.log(res)
        wx.hideLoading();
        let data = res.data || {}
        if (data && data.code == '200' && data.data) {
          let list = data.data.Data || []
          this.setData({
            showFFXX: true,
            ffxxData: list
          })
        } else {
          wx.showToast({
            title: data.msg || "网络异常！"
          })
        }

      },
      fail: (res) => {

        wx.hideLoading();
      }
    })
  },
  getFFXX(e) {
    let type = e.currentTarget.dataset.type
    let str = ''
    let fee = 1;
    switch (type) {
      case '1':
        str = '董监高付费查看'
        fee = parseFloat(this.data.fee.seniorPerson)
        break;
      case '2':
        str = '企业工商详情付费查看'
        fee = parseFloat(this.data.fee.basicDetails)
        break;
      case '3':
        str = '经营异常付费查看'
        fee = parseFloat(this.data.fee.exceptionCheck)
        break;
      case '4':
        str = '行政许可付费查看'
        fee = parseFloat(this.data.fee.adminLicenseCheck)
        break;
    }
    if (str) {
      this.setData({
        currentType: type
      })
      this.toGradePay(str, fee)
      // this.getSFData()

    }

  },

  onFFXXClose() {
    this.setData({
      showFFXX: false
    })
  },

  onCollectChange(options) {
    let collectValue = 0
    if (this.data.collectValue == 0) {
      collectValue = 1
    } else {
      collectValue = 0
    }
    const userid = wx.getStorageSync('wxUser') ? wx.getStorageSync('wxUser').id : ''
    if (!userid) {
      wx.switchTab({
        url: '/pages/my/index'
      })
    }
    wx.request({
      url: apiCollect.collectSaveUrl, //仅为示例，并非真实接口地址。
      data: {
        collectType: collectValue,
        createTime: '',
        enterpriseId: this.data.id,
        id: null,
        userId: userid
      },
      method: 'POST',
      success: (res) => {
        if (res.data.code === 200) {
          wx.showToast({
            title: '操作成功'
          });
          this.setData({
            collectValue: collectValue
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      id
    } = options
    this.setData({
      id
    })
    this.initDetail()

    //     const htmlSnip =
    // `<div class="div_class">
    //   <h1>Title</h1>
    //   <p class="p">
    //     Life is&nbsp;<i>like</i>&nbsp;a box of
    //     <b>&nbsp;chocolates</b>.
    //   </p>
    //   <div>31212421</div>
    // </div>
    // `
    // this.setData({
    //   htmlSnip:htmlSnip
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDictDataList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})